package cn.cttic.app.service.impl;

import cn.cttic.base.api.domain.BaseDevice;
import cn.cttic.base.api.domain.bo.BaseDeviceBo;
import cn.cttic.base.api.domain.vo.BaseDeviceVo;
import cn.hutool.core.bean.BeanUtil;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.app.mapper.BaseDeviceMapper;
import cn.cttic.app.service.IBaseDeviceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备信息Service业务层处理
 *
 * @author 刘伟
 * @date 2024-05-23
 */
@RequiredArgsConstructor
@Service
public class BaseDeviceServiceImpl implements IBaseDeviceService {

    private final BaseDeviceMapper baseMapper;

    /**
     * 查询设备信息
     */
    @Override
    public BaseDeviceVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询设备信息列表
     */
    @Override
    public TableDataInfo<BaseDeviceVo> queryPageList(BaseDeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseDevice> lqw = buildQueryWrapper(bo);
        Page<BaseDeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设备信息列表
     */
    @Override
    public List<BaseDeviceVo> queryList(BaseDeviceBo bo) {
        LambdaQueryWrapper<BaseDevice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseDevice> buildQueryWrapper(BaseDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseDevice> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), BaseDevice::getDeviceName, bo.getDeviceName());
        lqw.like(StringUtils.isNotBlank(bo.getDeviceModel()), BaseDevice::getDeviceModel, bo.getDeviceModel());
        lqw.between(params.get("beginDevicePrice") != null && params.get("endDevicePrice") != null,
            BaseDevice::getDevicePrice ,params.get("beginDevicePrice"), params.get("endDevicePrice"));
        lqw.gt(bo.getDeviceStockCount() != null, BaseDevice::getDeviceStockCount, bo.getDeviceStockCount());
        return lqw;
    }

    /**
     * 新增设备信息
     */
    @Override
    public Boolean insertByBo(BaseDeviceBo bo) {
        BaseDevice add = BeanUtil.toBean(bo, BaseDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备信息
     */
    @Override
    public Boolean updateByBo(BaseDeviceBo bo) {
        BaseDevice update = BeanUtil.toBean(bo, BaseDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseDevice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除设备信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
